﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace weba.Sales
{
    public partial class NeedMaterialDetailShow : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                if (Request.QueryString["NeedMaterialID"] != null)
                {
                    if (mk.IsNumber(Request.QueryString["NeedMaterialID"].ToString()))
                    {
                        //用一个隐藏Label保存传递过来的ID
                        this.lblNeedMaterialID.Text = Request.QueryString["NeedMaterialID"].ToString();
                    }
                    else
                    {
                        mk.Alert("参数错误！");
                    }
                }
                else
                {
                    mk.Alert("参数错误！");
                }

                //绑定页面基本数据
                this.BindData();

                ////设置经办人
                //UART.Accounts.Bus.User currentUser = (UART.Accounts.Bus.User)(Session["UserInfo"]);
                //this.lblCurrentUser.Text = currentUser.UserName;
                //this.lblManager.Text = currentUser.TrueName;
            }
        }
        private void BindData()
        {
         var  bllNeedHeader = mk.l.Sales_NeedMaterialHeader.ToList();
          Sales_NeedMaterialHeader mNeedHeader = bllNeedHeader.Where(h=>h.NeedMaterialID==int.Parse(this.lblNeedMaterialID.Text)).FirstOrDefault();
            if (mNeedHeader != null)
            {
                //设置经办人
              Accounts_Users currentUser = mk.l.Accounts_Users.FirstOrDefault();
                //设置根部门ID
                var bllDept = mk.l.Accounts_Department.ToList();
                 this.lblRootDepartmentID.Text = bllDept.Where(j=>j.ClassID==(int.Parse(currentUser.DepartmentID))).FirstOrDefault().RootID.ToString();
                if (int.Parse(this.lblRootDepartmentID.Text) == mNeedHeader.RootDepartmentID)
                {
                    var bllOrderHeader =mk.l.Sales_SalesOrderHeader.ToList();
                           Sales_SalesOrderHeader mOrderHeader = bllOrderHeader.Where(h=>h.SalesOrderID==mNeedHeader.SalesOrderID).FirstOrDefault();
                    if (mOrderHeader != null)
                    {
                        //不能修改已经结账完毕的订单

                        this.BindNeedMaterialDetail();
                        //判断当前用户有无财务管理员的角色，如没有，则不能查看合同金额
                       
                        
                            this.txtContractAmount.Text = mOrderHeader.ContractAmount.ToString();
                            this.txtBrokerage.Text = mOrderHeader.Brokerage.ToString();
                            this.txtSubTotal.Text = mOrderHeader.SubTotal.ToString();
                            this.txtTotalDue.Text = mOrderHeader.TotalDue.ToString();
                        
                        //如果订单已经流转，则把两个修改按钮设置为不可见 把关闭按钮设置为可见
                        //if (m.Status != "1")
                        //{
                        //    this.pnlAdd.Visible = false;
                        //    this.btnOK.Enabled = false;
                        //    this.btnOK.Visible = false;
                        //    this.btnSubmit.Enabled = false;
                        //    this.btnSubmit.Visible = false;
                        //    this.myGridView.Columns[6].Visible = false;
                        //}
                        //else
                        //{
                        //    this.btnOK.Enabled = true;
                        //    this.btnOK.Visible = true;
                        //    this.btnSubmit.Enabled = true;
                        //    this.btnSubmit.Visible = true;
                        //}
                        //}
                        //else
                        //{
                        //   mk.Alert("不能修改他人添加的订单！");
                        //}
                    }

                    else
                    {
                       mk.Alert("参数错误！");
                    }
                }
                else
                {
                   mk.Alert("只能查看本店的订单！");
                }
            }
            else
            {
               mk.Alert("参数错误！");
            }
        }

        /// <summary>
        /// Binds the need material detail.绑定用料单明细
        /// </summary>
        private void BindNeedMaterialDetail()
        {
            var bll = mk.l.Sales_NeedMaterialDetailandSales_Material.ToList();
           var ds = bll.Where(y=>y.NeedMaterialID.ToString()== this.lblNeedMaterialID.Text).ToList();
            this.myGridView.DataSource = ds;
            this.myGridView.DataBind();
        }
        /// <summary>
        /// Handles the RowDataBound event of the myGridView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
        protected void myGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //Label lblManufacturerCode = (Label)(e.Row.FindControl("lblManufacturerCode"));
                //UART.BLL.Sales.Manufacturer bllManufacturer = new UART.BLL.Sales.Manufacturer();
                //UART.Model.Sales.Manufacturer mManufacturer = bllManufacturer.Where(h=>h.int.Parse(this.myGridView.DataKeys[e.Row.RowIndex].Values["ManufacturerID"].ToString()));
                //lblManufacturerCode.Text = mManufacturer.ManufacturerCode;
            }
        }

    }
}
